package com.situ.user.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.situ.user.bean.GoodsSort;

public interface GoodsSortMapper {
	@Delete({ "delete from goods_sort", "where id = #{id,jdbcType=INTEGER}" })
	int deleteByPrimaryKey(Integer id);

	@Insert({ "insert into goods_sort ( name, ", "picture, parent)", "values ( #{name,jdbcType=VARCHAR}, ",
			"#{picture,jdbcType=VARCHAR}, #{parent,jdbcType=INTEGER})" })
	@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
	int insert(GoodsSort record);

	@Select({ "select", "id, name, picture, parent, status", "from goods_sort", "where id = #{id,jdbcType=INTEGER}" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "picture", property = "picture", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent", property = "parent", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
			@Result(column = "parent", property = "parentSort", one = @One(select = "selectByPrimaryKey")) })
	GoodsSort selectByPrimaryKey(Integer id);

	@Select({ "select", "id, name, picture, parent, status", "from goods_sort" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "picture", property = "picture", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent", property = "parent", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER) })
	List<GoodsSort> selectAll();

	@Update({ "<script>", "update goods_sort", "<set>", "<if test = 'name!=null'> name = #{name},</if>",
			"<if test = 'picture!=null'> picture = #{picture},</if>", "<if test = 'parent==null'> parent = null,</if>",
			"<if test = 'parent!=null'> parent = #{parent},</if>",
			"<if test = 'status!=null'> status = #{status},</if>", "</set>", "where id = #{id}", "</script>" })
	int updateByPrimaryKey(GoodsSort record);

	@Select({ "<script>", "select * from goods_sort", "<where>", "<if test = 'name!=null'> name like '%#{name}%' </if>",
			"<if test = 'parent==null'> and parent is null </if>",
			"<if test = 'parent!=null and parent!=0'> and parent = #{parent} </if>",
			"<if test = 'status!=null'> and status = #{status} </if>", "</where>", "</script>" })
	@Results({ @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
			@Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
			@Result(column = "picture", property = "picture", jdbcType = JdbcType.VARCHAR),
			@Result(column = "parent", property = "parent", jdbcType = JdbcType.INTEGER),
			@Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
			@Result(column = "parent", property = "parentSort", one = @One(select = "selectByPrimaryKey")),
			@Result(column = "id", property = "sonSorts", many = @Many(select = "selectByParentId"))

	})
	List<GoodsSort> select(GoodsSort goodsSort);

	// 根据父分类的id查询所有的子分类

	@Select("select * from goods_sort where parent = #{parentId} and status =0")
	List<GoodsSort> selectByParentId(Integer parentId);
}